<?php
/**
 * Created by PhpStorm.
 * User: Frings
 * Date: 2018/2/28
 * Time: 22:20
 */

$this->title = '商品分类';
$web = \yii\helpers\Url::base();
$url = \app\librarys\Tools::urlBase();

?>


<div class="main-title">
    <div class="title-txt"><?= $this->title ?></div>
</div>

<div class="panel panel-frame">
    <div class=" table-responsive">
        <table class="table table-bordered table-hover">
            <thead>
            <tr>
                <th>名称</th>
                <th>编码</th>
                <th>图片</th>
                <th>显示</th>
                <th>启用</th>
                <th>排序号</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
            <tr>
                <td style="text-align: left">商品分类</td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td class="table-text-align-left">
                    <a class="btn btn-link" href="javascript:onOperateData('add')">添加商品分类</a>
                </td>
            </tr>
            <?php foreach ($items

                           as $item): ?>
                <tr>
                    <td style="text-align: left;padding-left: <?= $item['level'] * 35 ?>px"><i
                                class="tier-icon"></i><?= $item['name'] ?></td>

                    <td>
                        <?= $item['code'] ?>
                    </td>

                    <td>
                        <?php if (!empty($item['image_id'])): ?>
                            <img src="<?= \app\models\File::getFile($item['image_id'])['url'] ?>"
                                 style="width: 50px;"
                                 onerror="showErrorImage(this)">
                        <?php endif ?>
                    </td>
                    <td><span class="label label-<?= $item['is_show'] == 1 ? 'success' : 'danger' ?>">
                            <?= $item['is_show'] == 1 ? '显示' : '不显示' ?>
                        </span></td>
                    <td><span class="label label-<?= $item['is_enabled'] == 1 ? 'success' : 'danger' ?>">
                            <?= $item['is_enabled'] == 1 ? '启用' : '不启用' ?>
                        </span></td>
                    <td><?= $item['sort'] ?></td>
                    <td class="table-text-align-left">


                        <a class="btn btn-link" href="javascript:onOperateData('edit','<?= $item['id'] ?>')">编辑</a>
                        <a class="btn btn-link" href="javascript:deleteCategory('<?= $item['id'] ?>')">删除</a>
                        <a class="btn btn-link" href="javascript:relateCategory('<?= $item['id'] ?>')">关联商品</a>
                        <?php
                        if ($item['level'] == 1) {
                            ?>
                            <a class="btn btn-link"
                               href="javascript:onOperateData('add','<?= $item['id'] ?>')">添加下级分类</a>
                            <?php
                        }
                        ?>
                    </td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>


<!--系统功能点添加 和编辑-->
<div class="modal fade" id="myModalEdit" tabindex="-1" role="dialog">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalEdit">商品分类</h4>
            </div>
            <div id="modaledit" class="modal-body">
            </div>
            <div class="modal-footer">
                <a class="btn btn-primary" onclick="saveData()"><i class="fa fa-save"></i> 保存
                </a>
                <a class="btn btn-cancel" data-dismiss="modal"><i class="fa fa-remove"></i> 取消
                </a>
            </div>
        </div>
    </div>
</div>


<div class="modal fade" id="myModalRelate" tabindex="-1" role="dialog">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span></button>
                <!--                <h4 class="modal-title" id="myModalRelate">关联商品</h4>-->
            </div>
            <div id="modalRelate" class="modal-body">
            </div>
            <div class="modal-footer">
                <a class="btn btn-primary" onclick="saveData()"><i class="fa fa-save"></i> 保存
                </a>
                <a class="btn btn-cancel" data-dismiss="modal"><i class="fa fa-remove"></i> 取消
                </a>
            </div>
        </div>
    </div>
</div>


<script type="text/javascript">

    function filter() {
        window.location.reload();
    }


    function onOperateData(opreate, id) {
        $("#myModalEdit").modal('show');
        var data = {};
        data.operate = opreate;
        if (id != undefined) {
            data.id = id;
        }

        modalEdit(data);
    }

    //打开新增修改页面
    function modalEdit(data) {
        $("#modaledit").html('正在加载数据...');

        requestHtml('category-partial', data, true, function (responseHtml) {
            $("#modaledit").html(responseHtml);
        });
    }

    var img_path;

    function setImgPath(path) {
        img_path = path;
    }

    function saveData() {
        // debugger;
        var id = $("#category_submit_form").find("#id").val();
        var parent_id = $("#category_submit_form").find("#parent_id").val().trim();
        var name = $("#category_submit_form").find("#name").val().trim();
        var code = $("#category_submit_form").find("#code").val().trim();
        var sort = $("#category_submit_form").find("#sort").val().trim();

        var is_show = 0;
        if ($("#category_submit_form").find("#is_show").is(':checked')) {
            is_show = 1;
        }

        var is_enabled = 0;
        if ($("#category_submit_form").find("#is_enabled").is(':checked')) {
            is_enabled = 1;
        }

        if (name == '' || name == null) {
            warningDialog('名称不能为空');
            return false;
        }

        if(parent_id.length < 1){
            parent_id = '0';
        }

        var data = {};
        data.id = id;
        data.parent_id = parent_id;
        data.name = name;
        data.code = code;
        data.is_show = is_show;
        data.is_enabled = is_enabled;
        data.sort = sort;
        data.img_path = img_path;


        requestJson("category-save-handler", data, true, function (response) {
            if (response.status == 0) {
                filter();
            } else {
                errorDialog(response.message);
            }
        });
    }

    function deleteCategory(id) {
        confirmDialog(id, '确认删除?', function (id) {
            var data = {};
            data.id = id;
            requestJson("category-delete-handler", data, true, function (response) {
                if (response.status == 0) {
                    filter();
                } else {
                    errorDialog(response.message, '删除失败！');
                }
            });
        });
    }

    function relateCategory(id) {
        $("#myModalRelate").modal('show');
        var data = {};
        data.category_id = id;
        var url = '<?= $url?>' + '/goods/category-goods-partial' + '?category_id=' + id;
//        requestHtml(url, data, true, function (responseHtml) {
//            $("#myModalRelate").html(responseHtml);
//        });

        var html = '<iframe style="width: 100%;height: 80%;" src='+url+'></iframe>';
        $("#myModalRelate").html(html);
    }
</script>
